var hasCycle = function (head) {
  let set = new Set();
  let p = head;
  while (p) {
    //存在重复
    if (set.has(p)) {
      return true;
    }
    set.add(p);
    p = p.next;
  }
  return false;
};

/**
 * 双指针解法
 * 快慢指针
 * @param {*} head 
 * @returns 
 */
var hasCycle = function (head) {
  if (!head || !head.next) {
    return false;
  }
  let fast = head.next.next;
  let slow = head;
  while (fast !== slow) {
    if (!fast || !fast.next) return false;
    fast = fast.next.next;
    slow = slow.next;
  }
  return true
};